package Eclass.Score;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import ibatis.IbatisHelper;

import com.ibatis.sqlmap.client.SqlMapClient;

public class ScoreManager {
	private SqlMapClient ibatis;

	public ScoreManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
	
	public List getBatch(){
		List result = null;
		try {
			result = ibatis.queryForList("score.getAllBatch","");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List getTrainerBatch(String userId){
		List result = null;
		try {
			result = ibatis.queryForList("score.getTrainerBatch", userId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}

	public List getSubject(String batch){
		List result = null;
		try {
			HashMap map = new HashMap();
			map.put("batch", batch);
			result = ibatis.queryForList("score.getAllSubject",map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List getTrainerSubject(String userId){
		List result = null;
		try {
			result = ibatis.queryForList("score.getTrainerSubject", userId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public BatchModel getBatchName(String batch){
		BatchModel bean = new BatchModel();
		try {
			bean = (BatchModel) ibatis.queryForObject("score.getBatchName", batch);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bean;
	}
	
	public SubjectModel getSubjectName(String subject){
		SubjectModel bean = new SubjectModel();
		try {
			bean = (SubjectModel) ibatis.queryForObject("score.getSubjectName", subject);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bean;
	}
	
	public ScoreModel getSessionDate(String session){
		ScoreModel bean = new ScoreModel();
		try {
			bean = (ScoreModel) ibatis.queryForObject("score.getSessionDate", session);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bean;
	}
	
	public BatchModel getLastBatch(){
		BatchModel bean = new BatchModel();
		try {
			bean = (BatchModel) ibatis.queryForObject("score.getLastBatch", "");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bean;
	}
	
	public TraineeModel getTraineeId(String traineeName){
		TraineeModel bean = new TraineeModel();
		try {
			bean = (TraineeModel) ibatis.queryForObject("score.getTraineeId", traineeName);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bean;
	}
	
	public TraineeModel getTraineeName(String userId){
		TraineeModel bean = new TraineeModel();
		try {
			bean = (TraineeModel) ibatis.queryForObject("score.getTraineeName", userId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bean;
	}
	
	public List getTraineeList(String batch){
		List traineeList = null;
		try {
			traineeList = ibatis.queryForList("score.getListTrainee", batch);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return traineeList;
	}
	
	public List getScoreList(String batch, String subject){
		List result = null;
		try {
			HashMap map = new HashMap();
			map.put("batch", batch);
			map.put("subject", subject);
			result = ibatis.queryForList("score.getScoreList", map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public List getScoreListForTrainee(String userId){
		List result = null;
		try {
			result = ibatis.queryForList("score.getScoreListTrainee", userId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public List getSessionList(String batch, String subject){
		List result = null;
		try {
			HashMap map = new HashMap();
			map.put("batch", batch);
			map.put("subject", subject);
			result = ibatis.queryForList("score.getClassSession", map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public List getScoreUpdateList(String batch, String subject, String sessionId){
		List result = null;
		try {
			HashMap map = new HashMap();
			map.put("batch", batch);
			map.put("subject", subject);
			map.put("session", sessionId);
			//System.out.println("session "+session+" subject "+subject+" batch "+batch);
			result = ibatis.queryForList("score.getUpdateScore", map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean updateScore(String sessionId, Integer traineeId, Double score, String usrupd){
		try {
			ibatis.startTransaction();
			HashMap map = new HashMap();
			map.put("session", sessionId);
			map.put("trainee", traineeId);
			map.put("score", score);
			map.put("usrupd", usrupd);
			this.ibatis.update("score.mergeScoreTable", map);
			ibatis.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
				//return false;
			}
		}
		return true;
	}
	
	public boolean verifiedScore(String sessionId, Integer traineeId){
		try {
			ibatis.startTransaction();
			HashMap map = new HashMap();
			map.put("session", sessionId);
			map.put("trainee", traineeId);
			this.ibatis.update("score.verifiedScore", map);
			ibatis.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
				//return false;
			}
		}
		return true;
	}
	
	public ScoreModel checkSessionScore(String batch, String subject){
		ScoreModel bean = new ScoreModel();
		try {
			HashMap map = new HashMap();
			map.put("batch", batch);
			map.put("subject", subject);
			bean = (ScoreModel) ibatis.queryForObject("score.checkSessionScore", map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bean;
	}
	
	public boolean calculateClassScore(String sessionId, Integer traineeId){
		try {
			ibatis.startTransaction();
			HashMap map = new HashMap();
			map.put("sessionId", sessionId);
			map.put("traineeId", traineeId);
			this.ibatis.update("score.calculateClassScore", map);
			ibatis.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
				//return false;
			}
		}
		return true;
	}
	
	public ScoreModel getBatchClassId(String sessionId){
		ScoreModel scoreModel = new ScoreModel();
		try {
			scoreModel = (ScoreModel) ibatis.queryForObject("score.getBatchClass", sessionId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return scoreModel;
	}
	
	public boolean calculateGrade(Integer batchClassId){
		try {
			ibatis.startTransaction();
			this.ibatis.update("score.calculateGrade", batchClassId);
			ibatis.commitTransaction();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
				//return false;
			}
		}
		return true;
	}
}
